package com.platform.car.app;

import com.platform.car.model.Parking;
import com.platform.car.model.Parkinglot;
import com.platform.car.model.Parkingpay;
import com.platform.car.model.Parkuse;
import com.platform.car.service.ParkingService;
import com.platform.car.service.ParkinglotService;
import com.platform.car.service.ParkingpayService;
import com.platform.car.service.ParkuseService;
import com.platform.upms.model.json.Tip;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 车位支付表管理 客户端 Controller
 *
 * @author GaoXiang
 * @version 1.0
 */
@Controller
@RequestMapping("/car/app/parkingpay")
public class AppParkingpayController {


    /**
     * 2017年5月31日 闫增宝  车位支付添加
     * @param parkingpay 携带parkuseId  预定ID  daylight白天时长  night夜晚时长  status支付状态  费用 payfee （计算）
     * @return 成功状态
     */
    @RequestMapping(value = "/save")
    @ResponseBody
    public Tip save(Parkingpay parkingpay) {
        Tip tip = new Tip();
        try {

            Parkuse parkuse = parkuseService.get(parkingpay.getParkuseId());
            if (parkuse != null) {
                Parkinglot parkinglot = parkinglotService.get(parkuse.getParkinglotId());
                Parking parking = parkingService.get(parkinglot.getParkingId());//停车场

                Double dayFee = parking.getDayFee();
                Double nightFee = parking.getNightFee();

                int light = parkingpay.getDaylight();
                int night = parkingpay.getNight();
                //计算费用
                Double dayFees = dayFee * light;
                Double nightFees = nightFee * night;
                parkingpay.setPayfee(dayFees + nightFees);//通过时长计算费用
                parkingpay.setStatus("0");
                parkingpayService.saveData(parkingpay);
                tip.setSuccess(true);
                tip.setData("保存成功！");

            } else {
                tip.setSuccess(false);
                tip.setData("没有相关信息！");
            }
            return tip;
        } catch (Exception e) {
            //e.printStackTrace();
            return new Tip(1, "保存失败！");
        }

    }


    /**
     * 查询支付列表 2017年6月2日  闫增宝
     * @param parkuse  携带accountId、车牌号
     * @return list
     */
    @RequestMapping(value = "/selectPayList")
    @ResponseBody
    public Tip selectPayList(Parkuse parkuse){
        List<Parkingpay> list = new ArrayList<>();
        parkuse.setStatus("3");
        List<Parkuse> parkuseList = parkuseService.selectByPlate(parkuse);
        if(parkuseList.size()>0){
            for (Parkuse p:parkuseList) {
                Parkingpay parkingpay = parkingpayService.selectParkuserId(p.getId());
                list.add(parkingpay);
            }
        }
        return new Tip(list);
    }



    //---------------------------- property -------------------------------

    @Resource
    private ParkinglotService parkinglotService;
    @Resource
    private ParkingpayService parkingpayService;
    @Resource
    private ParkingService parkingService;
    @Resource
    private ParkuseService parkuseService;

}
